﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Configuration;
using System.Reflection;
using System.IO;
using StepPrice;
using Tools;

namespace BLL.Business
{
    public class Common
    {
        public static void UpdateConfig(string xKey, string xValue)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings[xKey].Value = xValue;
            cfa.Save();
        }

        public static string ConvertEncoding(string str)
        {
            string s = "";
            try
            {
                Encoding utf8 = Encoding.UTF8;
                Encoding gb2312 = Encoding.Unicode;
                byte[] gb2312Bytes = gb2312.GetBytes(str);
                byte[] utf8Bytes = Encoding.Convert(gb2312, utf8, gb2312Bytes);

                char[] asciiChars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];

                s = new string(asciiChars);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ConvertEncoding方法-编码转换出现异常：" + ex.Message,ex);
            }
            
             return s;
        }

        private static int _threadCount;
        public static int ThreadCount
        {
            get { return _threadCount; }
            set { _threadCount = value;
            }
        }


        public static StepPriceInfo CalueStepInfo(string gasuserid, decimal payMoney, out int jtZGQL, out decimal currentJieYu)
        {
            jtZGQL = 0;
            currentJieYu = 0;
            int isStep = 0;   //是否收费阶梯；0：非阶梯；1：阶梯
            StepPriceInfo info = null;

            try
            {
                System.Data.DataSet ds = BLL.Db.StepDataManager.GetStepData(gasuserid, out jtZGQL,out isStep);

                if (ds != null && ds.Tables.Count > 0)
                {
                    if (isStep==0) //非阶梯
                    {
                        decimal payMoneyTemp = payMoney + decimal.Parse(ds.Tables[0].Rows[0]["AccountBalance"].ToString());
                        decimal price1 = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                        int gas = (int)(payMoneyTemp / price1);
                        decimal money = Math.Round(price1 * gas, 2);
                        currentJieYu = payMoneyTemp - money;
                        info = new StepPriceInfo()
                        {
                            Price1 = price1,
                            Gas1 = gas,
                            Money1 = money,
                            AccountBalance = currentJieYu,
                            PayMoney = payMoney,
                            GasAll = 0,
                            PayGas = gas
                        };
                    }
                    else   //阶梯
                    {
                        info = new StepPriceInfo()
                        {
                            //GasFlag:  0:设置的为年均用量，1：设置的为月均用量
                            FlagGasAver = Convert.ToInt32(ds.Tables[1].Rows[0]["GasFlag"]) == 0 ? true : false,
                            FlagPayWay = true,//按金额计算
                            LimitGas1 = int.Parse(ds.Tables[0].Rows[0]["GradeName"].ToString()),
                            LimitGas2 = int.Parse(ds.Tables[0].Rows[1]["GradeName"].ToString()),
                            Price1 = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString()),
                            Price2 = decimal.Parse(ds.Tables[0].Rows[1]["Price"].ToString()),
                            Price3 = decimal.Parse(ds.Tables[0].Rows[2]["Price"].ToString()),
                            StepStartDate = Convert.ToDateTime(ds.Tables[1].Rows[0]["StartDate"]),
                            StepEndDate = Convert.ToDateTime(ds.Tables[1].Rows[0]["EndDate"]),
                            OpenGasDate = Convert.ToDateTime(ds.Tables[1].Rows[0]["OpenGasDate"]).ToString("yyyyMMdd"),
                            GasAll = decimal.Parse(jtZGQL.ToString()),
                            PayGas = 0,
                            PayMoney = payMoney,
                            AccountBalance = decimal.Parse(ds.Tables[1].Rows[0]["AccountBalance"].ToString()),
                            RenDingGas = int.Parse(ds.Tables[1].Rows[0]["RDGasCountTotal"].ToString()),
                            RenDingShengYuGas = int.Parse(ds.Tables[1].Rows[0]["CurrentSurpluse"].ToString()),
                            JieZhuanGas = 0
                        };
                        if (int.Parse(ds.Tables[1].Rows[0]["RDGasCountTotal"].ToString()) > 0)
                        {
                            info.LeiJiGas1 = int.Parse(ds.Tables[1].Rows[0]["JTLJBuyGasCount1"].ToString());
                            info.LeiJiGas2 = int.Parse(ds.Tables[1].Rows[0]["JTLJBuyGasCount2"].ToString());
                            info.LeiJiGas3 = int.Parse(ds.Tables[1].Rows[0]["JTLJBuyGasCount3"].ToString());
                        }
                        var infoCalc = new CalcStepPrice(info).CalcStep();
                        info.Gas1 = infoCalc.Gas1;
                        info.Gas2 = infoCalc.Gas2;
                        info.Gas3 = infoCalc.Gas3;
                        info.PayGas = infoCalc.PayGas;
                        info.newAccountBalance = infoCalc.AccountBalance;//infoCalc.AccountBalance存储的为计算后的新结余
                        info.Money1 = infoCalc.Money1;
                        info.Money2 = infoCalc.Money2;
                        info.Money3 = infoCalc.Money3;
                        currentJieYu = infoCalc.AccountBalance;
                    }
                }
                return info;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
